﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.Pickinges
{
    public class PickingBackHandler : IRequestHandler<PickingBackCommand, ResultsDTO<Picking>>
    {

        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<Picking> PickingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="PickingRepository"></param>
        public PickingBackHandler(IBateRepository<Picking> PickingRepository)
        {
            this.PickingRepository = PickingRepository;
        }
        #endregion


        #region 生产领料 反填
        /// <summary>
        /// 生产领料 反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<Picking>> Handle(PickingBackCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO<Picking>对象result，并设置Code为 0
                ResultsDTO<Picking> result = new ResultsDTO<Picking>() { Code = 0 };
                
                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = $"select * from picking where PickingId = {request.PickingId} ";

                Picking picking = new Picking();
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //调用PickingRepository的GetAll方法 （）里面是执行查询并获取结果 能查询到的条件Picking数据存储在result对象中
                result.Objects = PickingRepository.GetAll(str, picking, conn).FirstOrDefault();
                result.Message = "生产领料反填成功";
                //result. = PickingRepository.GetAll(str, picking, conn).FirstOrDefault();

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
